//package org.activoricordi.app.model
package edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.model

import net.liftweb.mapper._
import _root_.java.math.MathContext
import xml.Text
import net.liftweb.common.Full
import  org.activoricordi.app.model._
import edu.ucdavis.fiehnlab.alchemy.alchemyAdmin.model._
import net.liftweb.http.S
import scala.util.matching.Regex

/**
 * Created with IntelliJ IDEA.
 * User: naru
 * Date: 3/27/13
 * Time: 3:35 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * define a Compound data Model
 */
class Compound extends LongKeyedMapper[Compound] with IdPK // with OneToMany[Long, Compound]{
{
  def getSingleton = Compound

  // def primaryKeyField = id
 // object id extends MappedLongIndex(this)
  object compoundName extends MappedString(this,200)
  object inchiKey extends MappedString(this,100)
  {
    /**
     *
     * constraint on inchikey format
     */

    override def validations = valMinLen(1,S.?("Inchikey name can't be null")) _ ::valRegex(java.util.regex.Pattern.compile("^([0-9A-Z\\-]+)$"),"Please Enter Valid InhiKey") _ ::v ::super.validations

  }
  object origin extends MappedString(this,100)
  object retentionTime extends MappedDecimal(this,MathContext.DECIMAL64,5)
  object theoreticalMass extends MappedDecimal(this,MathContext.DECIMAL64,5)
  object massWindowFactor extends MappedDecimal(this,MathContext.DECIMAL64,5)
  object expectedIntensity extends MappedDecimal(this,MathContext.DECIMAL64,5)
  object internalStandard extends MappedBoolean(this)
  object comments extends MappedTextarea(this,1000)
  object massSpectra extends MappedTextarea(this,1000000)

  /**
   * define one-to-Many relation between compound and methods
   */

  object method extends MappedLongForeignKey(this,Method) {
    override def dbIndexed_? = true
    override def validSelectValues = Full(Method.findAll.map(c =>(c.id.is,c.name.is)))
  }

  def methodName =
  Text("Method :" +(method.obj.map(_.name.is) openOr "Unknown"))
//  object methods extends MappedOneToMany(Method,Method.compound)


}
object Compound extends Compound with LongKeyedMetaMapper[Compound] {

  /**
   *
   * define database table name  for Model Compound
   */
  override def dbTableName = "compounds"
  override def fieldOrder = List(compoundName,inchiKey,origin,retentionTime,theoreticalMass,massWindowFactor,expectedIntensity,internalStandard,comments,massSpectra)
  // override def fieldOrder = List(name,riWindow,massWindow,description)
}
